<?php
    //会员注册
    function vip($username,$password,$realName,$address,$birthday,$phone){
       $mysql = new SaeMysql();
       $sql = "SELECT * FROM users WHERE username = '".$username."'";
       $result=$mysql->runSql($sql);
       if(count($result) > 0){
         return '{"errcode":1,"errmsg":"fail"}';
       }else{
        $sql = "INSERT  INTO users (username,password,realName,address,birthday,phone)VALUES('".$username."','".$password."','".$realName."','".$address."','".$birthday."','".$phone."')";
        $result=$mysql->runSql($sql);
         if($result){
         return '{"errcode":0,"errmsg":"ok"}';
         }else{
         return '{"errcode":2,"errmsg":"fail"}';
         }
         $mysql->closeDb();
       }   
    }
    //添加收货地址
    function recive($username,$phone,$re_address){
      $mysql = new SaeMysql();
      $uid = '1';
      $Sql = "INSERT  INTO recive_address (username,phone,re_address,uid)VALUES('".$username."','".$phone."','".$re_address."','".$uid."')";
      $result=$mysql->runSql($sql);
	  if($result){
         return '{"errcode":0,"errmsg":"ok"}';
      }else{
         return '{"errcode":1,"errmsg":"fail"}';
      }
      $mysql->closeDb();
    }
    //显示收货地址
    function show_address($openid){
       $mysql = new SaeMysql();
       $sql = "SELECT * FROM recive_address WHERE uid = '.$openid.'";
       $result = $mysql->runSql($sql);
       if(count($result) > 0){
         $sql = "SELECT username,phone,re_address WHERE uid = '.$openid.'";
         $result = $mysql->runSql($sql);
         if($result){
          $row = $result->fetch_assoc();
          $show = address($row['username'],$row['re_address'],$row[uid]);
          echo json_encode($show);
         }else{
          echo '{"errcode":2;"errmsg":"fail"}';
         }
       }else{
         return '{"errcode":1,"errmsg":"fail"}';
       }
    }
    //成为商家
    function mer_insert($realName,$id_card,$card_img,$photograph,$bank_card,$workers){
      $mysql = new SaeMysql();
       $sql = "SELECT * FROM merchant WHERE workers = '".$workers."'";
       $result = $mysql->getData($sql);
       if(count($result) > 0){
         return '{"errcode":1,"errmsg":"fail"}';
       }else{
        $sql = "INSERT  INTO users (realName,id_card,card_img,photograph,bank_card,workers)VALUES('".$realName."','".$id_card."','".$card_img."','".$photograph."','".$bank_card."','".$workers."')";
        $result = $mysql->runSql($sql);
        if($result){
         return '{"errcode":0,"errmsg":"ok"}';
         }else{
         return '{"errcode":2,"errmsg":"fail"}';
         }
		$mysql->closeDb(); 
       }  
    }
    //推广代理
    function age_insert($realName,$id_card,$card_img,$bank_card,$ag_grade){
        $mysql = new SaeMysql();
        $sql = "SELECT * FROM users WHERE id_card = '".$id_card."'";
        $result = $mysql->getData($sql);
        if(count($result) > 0){
         return '{"errcode":1,"errmsg":"fail"}';
        }else{
         $sql = "INSERT  INTO users (realName,id_card,card_img,bank_card,ag_grade)VALUES('".$realName."','".$id_card."','".$card_img."','".$bank_card."','".$ag_grade."')";
		 $result = $mysql->runSql($sql);
         if($result){
         return '{"errcode":0,"errmsg":"ok"}';
         }else{
         return '{"errcode":2,"errmsg":"fail"}';
         }
		 $mysql->closeDb();
        }     
    }
    //蛋糕添加
    function cake_insert($name,$type,$size,$price){
       $mysql = new SaeMysql();
	    for ($i=0; $i < count($arr); $i++) {
    if (@$arr[$i]->price) {
      // var_dump(@$arr[$i]->price);
       $size = @$arr[$i]->size;
       $price = @$arr[$i]->price;
       // 定义mysql对象，操作数据库
       $workers = "200";
      // $string = 'this is my name ' . $name . 'and my age' . $age
       $sql = 'select * from cake where size = "'.$size.'" and name = "'.$name.'" and workers = "200" and type= "'.$typer.'" ';
      //  $sql = "select * from cake where size = $size and name = $name and workers = '200' and  type= $typer ";
       $result = $mysql->query($sql);
       var_dump($mysql->query($sql));
       if ($result->num_rows > 0) {
        //  $sql = "update cake set price = '$price' where size = '$size' and name = '$name' and workers = '200' and  type= '$typer' ";
         $sql = 'update cake set price = "'.$price.'" where size = "'.$size.'" and name = "'.$name.'" and workers = "200" and  type= "'.$typer.'"';
         echo $sql;
         $result = $mysql->query($sql);
        //  echo '{"errcode":0,"errmsg":"fail", "a":"'.$sql.'"}';
         if ($result) {
          $GLOBALS['a'] = true;
        }else{
         $GLOBALS['a'] = false;
           break;
         }
       }else{
         // 为防止重复注册，或者注册失败，应该先查询数据库用户信息表中是否已经存在该账户信息，如果不存在再进行注册操作
         $sql = "insert into cake (name,type,price,size,workers) values ('$name','$typer','$price','$size','$workers')";
         $mysql->query("set names utf8");
         $result = $mysql->query($sql);
         $GLOBALS['a'] = true;
        if ($result) {
            $GLOBALS['a'] = true;
        }else{
          $GLOBALS['a'] = false;
          break;
        }
       }

    }
 }
 if ($GLOBALS['a']) {
   echo '{"errcode":1,"errmsg":"ok"}';
 }else {
  echo '{"errcode":0,"errmsg":"fail", "a":"'.$GLOBALS['b'].'"}';
 }
    }
 
    
    //蛋糕查询
    function cake_select(){
      $mysql = new SaeMysql();
      $sql = "SELECT * FROM cake WHERE 1";
      $result = $mysql->getData($sql);
		if($mysql->errno() != 0 )
		{
		    die( "Error:".$mysql->errmsg() );
		}
      $mysql->closeDb();
      return $result;
    }
    /*蛋糕分类查询 大小 类型 价格*/
    function cake_type_select($size,$type,$price){
      $mysql = new SaeMysql();
      $Sql = "SELECT * FROM cake WHERE size='".$size."' or type='".$type."' or price='".$price."'";
      $result = $mysql->getData($sql);
		if($mysql->errno() != 0 )
		{
		    die( "Error:".$mysql->errmsg() );
		}
      $mysql->closeDb();
      return $result;
    }
    //标记蛋糕可积分兑换
    function cake_sign($id,$true){
      $mysql = new SaeMysql();
      $sql   = "UPDATE cake SET sign = '".$true."' WHERE id = '".$id."'";
      $result = $mysql->runSql($sql);
      $mysql->closeDb();
    }
    //积分查询
    function integral($username){
      $mysql = new SaeMysql();
      $sql = "SELECT * FROM vip WHERE username = '".$username."'";
      $result = $mysql->getData($sql);
		if($mysql->errno() != 0 )
		{
		    die( "Error:".$mysql->errmsg() );
		}
      $mysql->closeDb();
      // 返回的数组 再做测试 未完
      return $result;
    }
    //可积分兑换蛋糕查询
    function cake_integral($true){
      $mysql = new SaeMysql();
      $sql = "SELECT * FROM cake WHERE sign = '".$true."'";
      $result = $mysql->getData($sql);
		if($mysql->errno() != 0 )
		{
		    die( "Error:".$mysql->errmsg() );
		}
      $mysql->closeDb();
      // 返回的数组 再做测试 未完
      return $result;
    }
	//微信信息
 	function insert($nickname,$openid,$headimgurl){
		$mysql = new SaeMysql();
		$sql = "INSERT  INTO users (nickname,openid,headimgurl)VALUES('".$nickname."','".$openid."','".$headimgurl."')";
		$mysql->runSql($sql);
		if($mysql->errno() != 0 )
		{
		    die( "Error:".$mysql->errmsg() );
		}
		$mysql->closeDb();
    }
    //乐趣分享添加
    function share_insert($title,$content,$share_img,$wid){
       $mysql = new SaeMysql();
		$sql = "INSERT  INTO share (title,content,comment,share_img,wid)VALUES('".$title."','".$content."','".$share_img."''".$wid."')";
		$mysql->runSql($sql);
		if($mysql->errno() != 0 )
		{
		    die( "Error:".$mysql->errmsg() );
		}
		$mysql->closeDb();
    }
    //查询所有分享
    function share_select(){
       $mysql = new SaeMysql();
       $sql = "SELECT * FROM share WHERE 1";
       $result = $mysql->getData($sql);
       if($mysql->errno() != 0 )
		{
		    die( "Error:".$mysql->errmsg() );
		}
       $mysql->closeDb();
       // 返回的数组 再做测试 未完
       return $result;
    }
    //评论添加
    function comment_insert($com_content,$sid,$wid){
      $mysql = new SaeMysql();
		$sql = "INSERT  INTO share (com_content,sid,wid)VALUES('".$com_content."','".$sid."','".$wid."')";
		$mysql->runSql($sql);
		if($mysql->errno() != 0 )
		{
		    die( "Error:".$mysql->errmsg() );
		}
		$mysql->closeDb();
    }
    //查询所有评论
    function comment_select(){
     $mysql = new SaeMysql();
       $sql = "SELECT * FROM comment WHERE 1";
       $result = $mysql->getData($sql);
       if($mysql->errno() != 0 )
		{
		    die( "Error:".$mysql->errmsg() );
		}
       $mysql->closeDb();
       // 返回的数组 再做测试 未完
       return $result;
    }
	//查询数据函数
	function select($openid){
    	$mysql = new SaeMysql();
        $sql = "SELECT * FROM users WHERE openid = '".$openid."'";
        $result = $mysql->getData($sql);
        if($mysql->errno() != 0 )
		{
		    die( "Error:".$mysql->errmsg() );
		}
       $mysql->closeDb();
       return $result;
    }

?>